#include<iostream>
#include<stdio.h>
#include<sys/time.h>
#include<unistd.h>

using namespace std;
const int n=500;
int main(){
        double time1 = 0,time2=0;
        for(int t=0;t<100;t++){
        int a[n][n];
        int b[n];
        int sum[n];
        for(int i=0;i<n;i++){
                b[i]=2*i;
                sum[i]=0;
                for(int j=0;j<n;j++)
                        a[i][j]=i+j+1;
        }

        struct  timeval  tv_begin,tv_end,tv_begin1,tv_end1;
        gettimeofday(&tv_begin1,NULL);
        for(int i=0;i<n;i++){//common
                for(int j=0;j<n;j++)
                        sum[i]+=a[i][j]*b[j];
        }
        gettimeofday(&tv_end1,NULL);
        time1+=(tv_end1.tv_sec-tv_begin1.tv_sec)*1000000+(tv_end1.tv_usec-tv_begin1.tv_usec);

        for(int i=0;i<n;i++)
                sum[i]=0;

        gettimeofday(&tv_begin,NULL);
        for(int j=0;j<n;j++){//cache
                for(int i=0;i<n;i++)
                        sum[i]+=a[i][j]*b[j];
        }
        gettimeofday(&tv_end,NULL);
        time2+=(tv_end.tv_sec-tv_begin.tv_sec)*1000000+(tv_end.tv_usec-tv_begin.tv_usec);

        }
        cout<<"common: "<<time2/100<<endl;
        cout<<"cache:"<<time1/100<<endl;
        //for(int i=0;i<50;i++)
                //cout<<sum[i]<<" ";
        return 0;
}
